package com.example.campsystem.controller;

import com.example.campsystem.entity.CustomApi;
import com.example.campsystem.entity.Bookings;
import com.example.campsystem.service.BookingsService;
import com.example.campsystem.service.CampsService;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/booking")

public class BookingController {
    @Autowired
    private BookingsService bookingService;
    @Autowired
    private CampsService campsService;
    private Logger logger = LoggerFactory.getLogger(BookingController.class);
    //根据用户id创建订单
    @PostMapping("/create")
    public ResponseEntity<CustomApi> createBooking(@RequestBody Bookings bookings){
        CustomApi customApi = new CustomApi();
        logger.info("创建订单：{}", bookings);
        try {
            bookingService.createBooking(bookings);
            customApi.setData(null);
            customApi.setCode(200);
            customApi.setMsg("创建成功");
            return ResponseEntity.ok(customApi);
        } catch (Exception e) {
            customApi.setCode(500);
            customApi.setMsg("创建失败：" + e.getMessage());
            customApi.setData(null);
            return ResponseEntity.ok(customApi);
        }
    }
    @GetMapping("/list")
    public ResponseEntity<CustomApi> getBookingList(@RequestParam Long userId){
        CustomApi customApi = new CustomApi();
        logger.info("获取用户订单列表：{}", userId);
        try {
            List<Map<String, Object>> bookingsList = campsService.selectlistbyuserid(userId);
            customApi.setData(bookingsList);
            customApi.setCode(200);
            customApi.setMsg("获取成功");
            return ResponseEntity.ok(customApi);
        } catch (Exception e) {
            customApi.setCode(500);
            customApi.setMsg("获取失败：" + e.getMessage());
            customApi.setData(null);
            return ResponseEntity.ok(customApi);
        }
    }
    //修改订单状态
    @PostMapping("/update")
    public ResponseEntity<CustomApi> updateBookingStatus(@RequestBody Bookings bookings){
        CustomApi customApi = new CustomApi();
        logger.info("修改订单状态：{}", bookings);
        try {
            boolean updated = bookingService.updateBookingStatus(bookings);
            if (updated) {
                customApi.setCode(200);
                customApi.setMsg("修改成功");
            } else {
                customApi.setCode(400);
                customApi.setMsg("修改失败，订单不存在");
            }
            return ResponseEntity.ok(customApi);
        } catch (Exception e) {
            customApi.setCode(500);
            customApi.setMsg("修改失败：" + e.getMessage());
            return ResponseEntity.ok(customApi);
        }
    }
    //删除订单根据userid和campid
    @PostMapping("/delete")
    public ResponseEntity<CustomApi> deleteBooking(@RequestBody Bookings bookings){
        CustomApi customApi = new CustomApi();
        logger.info("删除订单：{}", bookings);
        try {
            boolean deleted = bookingService.deleteBooking(bookings);
            if (deleted) {
                customApi.setCode(200);
                customApi.setMsg("删除成功");
            } else {
                customApi.setCode(400);
                customApi.setMsg("删除失败，订单不存在");
            }
            return ResponseEntity.ok(customApi);
        } catch (Exception e) {
            customApi.setCode(500);
            customApi.setMsg("删除失败：" + e.getMessage());
            return ResponseEntity.ok(customApi);
        }
    }

}

